package online.liulei.design.mode;

public class WinnerState implements State{
    private GumballMachine gumballMachine;

    public WinnerState(GumballMachine gumballMachine){
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("Please wait, we're already giving a gumball.");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("Sorry, you already turned the crank.");
    }

    @Override
    public void turnCrank() {
        System.out.println("Turning twice doesn't get you another gumball.");
    }

    @Override
    public void dispense() {
        System.out.println("YOU ARE A WINNER! You get two gumballs for your quarter.");
        gumballMachine.releaseBall();
        if(gumballMachine.getCount() <= 0){
            gumballMachine.setState(gumballMachine.getSoldOutState());
            return;
        }
        gumballMachine.releaseBall();
        if(gumballMachine.getCount() <= 0){
            System.out.println("Out of gumballs!");
            gumballMachine.setState(gumballMachine.getSoldOutState());
            return;
        }
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }
}
